﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.LeaveCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.LeaveHandler
{
    public class UpdLeaveHandler : IRequestHandler<UpdLeaveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Leave> leaveRep;
        private readonly IMapper mapper;

        public UpdLeaveHandler(IBaseRepository<Leave> leaveRep,IMapper mapper)
        {
            this.leaveRep = leaveRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(UpdLeaveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = mapper.Map<Leave>(request);
            if (model != null) 
            {
                model.UpdateTime = DateTime.Now;
                leaveRep.Update(model);
                result.Code=APIEnum.Success;
                result.Msg = "修改成功";
                result.Data = 1;
            }
            else
            {
                result.Code=APIEnum.Failed;
                result.Msg = "修改失败";
                result.Data = -1;
            }
           
            return Task.FromResult(result);
        }
    }
}
